home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / demoversionen / upsey / src / database.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  7KB  |  318 lines

  1.  
  2.  
  3. #include <dos/dos.h>
  4. #include <exec/types.h>
  5. #include <exec/lists.h>
  6. #include <exec/nodes.h>
  7. #include <exec/libraries.h>
  8. #include <libraries/commodities.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <functions.h>
  13.  
  14. /*
  15.  *  Application specific include files
  16.  */
  17. #include "ups.h"
  18. #include "main.h"
  19. #include "database.h"
  20. #include "functions.h"
  21.  
  22. char *your_area = NULL;
  23. char *zip_codes = NULL;
  24.  
  25.  
  26. BOOL
  27. GetRecordList (struct List *record_list, STRPTR database)
  28. {
  29.   int retval = FALSE;
  30.   char buf[512];
  31.  
  32.   LONG error = FALSE;
  33.   struct AnchorPath *anchorpath;
  34.  
  35.   if (anchorpath = AllocMem (sizeof (struct AnchorPath) + 512, MEMF_CLEAR))
  36.     {
  37.       anchorpath->ap_Strlen = 512;
  38.  
  39.       if (database)
  40.     {
  41.       strcpy (buf, database);
  42.       AddPart (buf, "#?.ups", 512);
  43.  
  44.       if ((error = MatchFirst (buf, anchorpath)) == 0)
  45.         strcpy (buf, anchorpath->ap_Buf);
  46.  
  47.       MatchEnd (anchorpath);
  48.     }
  49.       else
  50.     {
  51.       strcpy (buf, "#?.ups");
  52.  
  53.       if ((error = MatchFirst (buf, anchorpath)) == 0)
  54.         strcpy (buf, anchorpath->ap_Buf);
  55.  
  56.       MatchEnd (anchorpath);
  57.  
  58.       if (error)
  59.         {
  60.           strcpy (buf, "s:");
  61.           AddPart (buf, "#?.ups", 512);
  62.  
  63.           if ((error = MatchFirst (buf, anchorpath)) == 0)
  64.         strcpy (buf, anchorpath->ap_Buf);
  65.  
  66.           MatchEnd (anchorpath);
  67.         }
  68.     }
  69.  
  70.       FreeMem (anchorpath, sizeof (struct AnchorPath) + 512);
  71.  
  72.     }
  73.   else
  74.     {
  75.       error = TRUE;
  76.     }
  77.  
  78.   if (!error)
  79.     {
  80.       FILE *fd;
  81.  
  82.       if (fd = fopen ((char *) buf, "r"))
  83.     {
  84.       while (fgets (buf, 256, fd))
  85.         {
  86.           char *field1, *field2, *field3, *field4, *field5, *field6;
  87.           char *ptr;
  88.           int index, i;
  89.  
  90.           if (strchr (buf, ';'))
  91.         {
  92.           {
  93.             char *start_ptr, *end_ptr;
  94.  
  95.             if (start_ptr = strdup (buf))
  96.               {
  97.             strlwr (start_ptr);
  98.  
  99.             if ((end_ptr = strstr (start_ptr, "area:")) || (end_ptr = strstr (start_ptr, "code:")))
  100.               index = end_ptr - start_ptr;
  101.             else
  102.               index = 0;
  103.  
  104.             for (ptr = strtok (buf + index, ":"); ptr; ptr = strtok (NULL, " \t\n"))
  105.               {
  106.                 if (strnicmp (ptr, "AREA", 5) == 0)
  107.                   {
  108.                 if (your_area)
  109.                   free (your_area);
  110.  
  111.                 ptr = strtok (NULL, "\n");
  112.  
  113.                 while (*ptr == ' ')
  114.                   ptr++;
  115.  
  116.                 your_area = strdup (ptr);
  117.                   }
  118.  
  119.                 if (strnicmp (ptr, "CODE", 6) == 0)
  120.                   {
  121.                 if (zip_codes)
  122.                   free (zip_codes);
  123.  
  124.                 ptr = strtok (NULL, "\n");
  125.  
  126.                 while (*ptr == ' ')
  127.                   ptr++;
  128.  
  129.                 zip_codes = strdup (ptr);
  130.                   }
  131.               }
  132.  
  133.             free (start_ptr);
  134.               }
  135.           }
  136.  
  137.           while (ptr = strrchr (buf, ';'))
  138.             *ptr = '\000';
  139.         }
  140.  
  141.           for (ptr = strtok (buf, " \t\n"), i = 0; ptr && (i <= 5); ptr = strtok (NULL, " \t\n"), i++)
  142.         {
  143.           switch (i)
  144.  
  145.             {
  146.             case 0:
  147.               field1 = strdup (ptr);
  148.               break;
  149.  
  150.             case 1:
  151.               field2 = strdup (ptr);
  152.               break;
  153.  
  154.             case 2:
  155.               field3 = strdup (ptr);
  156.               break;
  157.  
  158.             case 3:
  159.               field4 = strdup (ptr);
  160.               break;
  161.  
  162.             case 4:
  163.               field5 = strdup (ptr);
  164.               break;
  165.  
  166.             case 5:
  167.               field6 = strdup (ptr);
  168.               break;
  169.             }
  170.         }
  171.  
  172.           if (i == 5 || i == 6)
  173.         {
  174.           struct record *current_record;
  175.  
  176.           if (current_record = (struct record *) malloc (sizeof (struct record)))
  177.             {
  178.               if (i == 5)
  179.             {
  180.               current_record->rec_Field1 = field1;
  181.  
  182.               current_record->rec_Field2 = NULL;
  183.  
  184.               current_record->rec_Field3 = field2;
  185.  
  186.               current_record->rec_Field4 = field3;
  187.  
  188.               current_record->rec_Field5 = field4;
  189.  
  190.               current_record->rec_Field6 = field5;
  191.  
  192.               {
  193.                 char *ptr;
  194.  
  195.                 if (ptr = (char *) malloc (20))
  196.                   {
  197.                 sprintf (ptr, "%s %s", current_record->rec_Field1, current_record->rec_Field2);
  198.                 current_record->rec_Node.ln_Name = ptr;
  199.                   }
  200.               }
  201.  
  202.               AddTail (record_list, (struct Node *) current_record);
  203.             }
  204.               else if (i == 6)
  205.             {
  206.               current_record->rec_Field1 = field1;
  207.  
  208.               current_record->rec_Field2 = field2;
  209.  
  210.               current_record->rec_Field3 = field3;
  211.  
  212.               current_record->rec_Field4 = field4;
  213.  
  214.               current_record->rec_Field5 = field5;
  215.  
  216.               current_record->rec_Field6 = field6;
  217.  
  218.               {
  219.                 char *ptr;
  220.  
  221.                 if (ptr = (char *) malloc (20))
  222.                   {
  223.                 sprintf (ptr, "%s %s", current_record->rec_Field1, current_record->rec_Field2);
  224.                 current_record->rec_Node.ln_Name = ptr;
  225.                   }
  226.               }
  227.  
  228.               AddTail (record_list, (struct Node *) current_record);
  229.             }
  230.  
  231.               retval = TRUE;
  232.             }
  233.         }
  234.         }
  235.       fclose (fd);
  236.     }
  237.     }
  238.  
  239.   return (retval);
  240. }
  241.  
  242. VOID
  243. FreeRecordList (struct List * record_list)
  244. {
  245.   struct record *current_record;
  246.  
  247.   if (your_area)
  248.     free (your_area);
  249.  
  250.   if (zip_codes)
  251.     free (zip_codes);
  252.  
  253.   while (current_record = (struct record *) RemHead (record_list))
  254.     {
  255.       if (current_record->rec_Node.ln_Name)
  256.     free (current_record->rec_Node.ln_Name);
  257.  
  258.       if (current_record->rec_Field1)
  259.     free (current_record->rec_Field1);
  260.  
  261.       if (current_record->rec_Field2)
  262.     free (current_record->rec_Field2);
  263.  
  264.       if (current_record->rec_Field3)
  265.     free (current_record->rec_Field3);
  266.  
  267.       if (current_record->rec_Field4)
  268.     free (current_record->rec_Field4);
  269.  
  270.       if (current_record->rec_Field5)
  271.     free (current_record->rec_Field5);
  272.  
  273.       if (current_record->rec_Field6)
  274.     free (current_record->rec_Field6);
  275.  
  276.       free (current_record);
  277.     }
  278. }
  279.  
  280. struct record *
  281. GetRecord (struct List *record_list, int record_num)
  282. {
  283.   struct record *which_record;
  284.  
  285.   which_record = (struct record *) record_list->lh_Head;
  286.   {
  287.     int x;
  288.  
  289.     for (x = 0; x < record_num; x++)
  290.       which_record = (struct record *) which_record->rec_Node.ln_Succ;
  291.   }
  292.   return (which_record);
  293. }
  294.  
  295. VOID
  296. DisplayRecord (int record_num)
  297. {
  298.   {
  299.     struct record *selected_record;
  300.  
  301.     if (selected_record = (struct record *) GetRecord (&record_list, record_num))
  302.       {
  303.     GT_SetGadgetAttrs (Project0Gadgets[7], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Node.ln_Name, TAG_DONE);
  304.  
  305.     GT_SetGadgetAttrs (Project0Gadgets[2], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Field3, TAG_DONE);
  306.     GT_SetGadgetAttrs (Project0Gadgets[3], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Field4, TAG_DONE);
  307.     GT_SetGadgetAttrs (Project0Gadgets[4], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Field5, TAG_DONE);
  308.     GT_SetGadgetAttrs (Project0Gadgets[5], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Field6, TAG_DONE);
  309.       }
  310.   }
  311.  
  312.   if (your_area)
  313.     GT_SetGadgetAttrs (Project0Gadgets[0], Project0Wnd, NULL, GTTX_Text, your_area, TAG_DONE);
  314.  
  315.   if (zip_codes)
  316.     GT_SetGadgetAttrs (Project0Gadgets[1], Project0Wnd, NULL, GTTX_Text, zip_codes, TAG_DONE);
  317. }
  318.